package Kotlin

import Entity.IdCard
import Entity.Student
import Many2Many.Teacher
import ManyToOne.Classroom
import com.alibaba.fastjson.JSON
import com.alibaba.fastjson.serializer.SerializerFeature
import org.hibernate.Session
import org.hibernate.SessionFactory
import org.hibernate.Transaction
import org.hibernate.cfg.Configuration
import org.joda.time.DateTime
import org.junit.After
import org.junit.Before
import org.junit.Test
import java.sql.Timestamp

/**
 * Created by futao on 2017/9/27.
 */
class dbTest {
    lateinit var configuration: Configuration
    lateinit var sessionFactory: SessionFactory
    lateinit var session: Session
    lateinit var transation: Transaction

    @Before
    fun initEnvironment() {
        configuration = Configuration().configure()
        sessionFactory = configuration.buildSessionFactory()
        session = sessionFactory.openSession()
        transation = session.beginTransaction()
    }

    @After
    fun recycleResource() {
        transation.commit()
        session.close()
        sessionFactory.close()
    }

    /**
     * 一个事务在这里怎么理解
     * 为什么后面的save操作失败了，但是前面的IdCard的save操作还是成功了
     */
    @Test
    fun dbtest() {
        session.save(IdCard().apply {
            pid = "11"
            sname = "熊anger"
            address = "上海小二房"
        })
        session.save(Student().apply {
            gender = "男"
            birthday = Timestamp(DateTime.now().millis)
            major = "计算机"
            idCard = session.get(Student::class.java, 2) as IdCard
        })
    }

    @Test
    fun dbTest2() {
        println(JSON.toJSONString(session.get(IdCard::class.java, "11") as IdCard, SerializerFeature.PrettyFormat))
//        val entity=session.get(IdCard::class.java, "11") as IdCard
//        entity.address="老北京四合院"
//        session.update(entity)

        session.delete(session.get(IdCard::class.java,"11"))
    }

    @Test
    fun manyToOneTest(){
        val classroom=Classroom().apply {
            cid="4444"
            cname="微创班"
        }
        session.save(classroom)
        session.save(ManyToOne.Student().apply {
            gender="男"
            birthday= Timestamp(DateTime.now().millis)
            major="计算机专业"
            setClassroom(classroom)
        })
    }

    @Test
    fun many2many(){
        val t1=Teacher().apply {
            tid="T001"
            tname="张老师"
        }
        val t2=Teacher().apply {
            tid="t999"
            tname="吴彦祖"
        }

        val teachers=HashSet<Teacher>()
        teachers.add(t1)
        teachers.add(t2)

        val s1=Many2Many.Student().apply {
            name="熊老一"
            gender="男"
            birthday= Timestamp(DateTime.now().millis)
            major="计算机"
            teacherSet=teachers
        }
        val s2= Many2Many.Student().apply {
            name="熊老二"
            gender="男"
            birthday= Timestamp(DateTime.now().millis)
            major="软件工程"
        }
        val s3=Many2Many.Student().apply {
            name="李子"
            gender="女"
            birthday= Timestamp(DateTime.now().millis)
            major="建筑学"
            teacherSet=teachers
        }

        session.save(t1)
        session.save(t2)
        session.save(s1)
        session.save(s2)
        session.save(s3)
    }
}